export const isValue = (val: any) => {
  if (val === null || val === undefined || (typeof val === 'string' && val.trim() === '') || (typeof val === 'object' && val?.length === 0)) {
    return false
  }
  return true
}

export const structureParams = (array: Array<any>, attribute: string) => {
  const endArray: any[] = []
  array.forEach(v => {
    endArray.push(v[attribute])
  })
  return endArray
}

export const replaceParams = (array: Array<any>, reArray: Array<any>, attribute: string, reAttribute: string) => {
  const endArray: any[] = []
  const endAllArray: any[] = []
  array.forEach(v => {
    reArray.forEach(rv => {
      if (v === rv[attribute]) {
        endArray.push(rv[reAttribute])
        endAllArray.push(rv)
      }
    })
  })
  return {
    replace: endArray,
    all: endAllArray
  }
}

export const copyObject = (ob: Object) => {
  return JSON.parse(JSON.stringify(ob))
}

export const arrayToMap = (array: Array<any>, key: any) => {
  const map = new Map()
  array.forEach((v: any) => {
    map.set(v[key], v)
  })
  return map
}

/**
 * 通过某个字段在一个多级数组中查找数据
 * @param data 目标数组，不能包含函数
 * @param current 目标数据
 * @param key 查找的字段
 * @param children 子集集合字段
 */
export const findInListData = (data: Array<any>, current:any, key = "id", children = 'children') => {

  for(let item of data){

    if(item[key] && JSON.parse(JSON.stringify(item[key])) == JSON.parse(JSON.stringify(current))) return item

    if(!!item[children] && Array.isArray(item[children]) && item[children].length > 0){

      const findChildData: any = findInListData(item[children], current, key, children)

      if(findChildData) return findChildData

    }

  }

  return null
}

export const formatGetParam = (params: any) => {
  let paramUrl = ''
  Object.keys(params).forEach((v, i) => {
    paramUrl += i === 0 ? `${v}=${encodeURIComponent(params[v])}` : `&${v}=${encodeURIComponent(params[v])}`
  })
  return paramUrl
}

export const formatTableHeadFilters = (arr: Array<any>, text = 'dictLabel', value = 'dictValue') => {
  return arr.map(v => {
    v.value = v[value]
    v.text = v[text]
    return v
  })
}

export const YMDHms = (date: any) => {
  const _date = new Date(date)
  const Y = `${_date.getFullYear()}`;
  const M = `${_date.getMonth() + 1 < 10 ? `0${_date.getMonth() + 1}` : _date.getMonth() + 1}`;
  const D = `${_date.getDate() < 10 ? `0${_date.getDate()}` : _date.getDate()}`;
  const H = `${_date.getHours() < 10 ? `0${_date.getHours()}` : _date.getHours()}`;
  const m = `${_date.getMinutes() < 10 ? `0${_date.getMinutes()}` : _date.getMinutes()}`;
  const s = _date.getSeconds() < 10 ? `0${_date.getSeconds()}` : _date.getSeconds();
  return `${Y}-${M}-${D} ${H}:${m}:${s}`;
}

export const YM = (date: any, format = false) => {
  const _date = new Date(date)
  const Y = `${_date.getFullYear()}`;
  const M = `${_date.getMonth() + 1 < 10 ? `0${_date.getMonth() + 1}` : _date.getMonth() + 1}`;
  return format ? `${Y}年${M}月` : `${Y}-${M}`;
}
export const M = (date: any, format = false) => {
  const _date = new Date(date)
  const M = `${_date.getMonth() + 1 < 10 ? `0${_date.getMonth() + 1}` : _date.getMonth() + 1}`;
  return format ? `${M}月` : `${M}`;
}

export const YMD = (date: any, format = false) => {
  const _date = new Date(date)
  const Y = `${_date.getFullYear()}`;
  const M = `${_date.getMonth() + 1 < 10 ? `0${_date.getMonth() + 1}` : _date.getMonth() + 1}`;
  const D = `${_date.getDate() < 10 ? `0${_date.getDate()}` : _date.getDate()}`;
  return format ? `${Y}年${M}月${D}日` : `${Y}-${M}-${D}`;
}

export const Hms = (date: any, format = false) => {
  const _date = new Date(date)
  const H = `${_date.getHours() < 10 ? `0${_date.getHours()}` : _date.getHours()}`;
  const m = `${_date.getMinutes() < 10 ? `0${_date.getMinutes()}` : _date.getMinutes()}`;
  const s = _date.getSeconds() < 10 ? `0${_date.getSeconds()}` : _date.getSeconds();
  return format ? `${H}时${m}分${s}秒` : `${H}:${m}:${s}`;
}

//防抖
export const debounce = function (cb: any, ms = 0) {
  let timer: any = null
  return function () {
    if (timer) clearTimeout(timer)
    timer = setTimeout(() => {
      //  @ts-ignore
      cb.apply(this, arguments)
      timer = null
    }, ms)
  }
}

export const comTime = (time: any, format = false) => {
  const sAll = time
  const d = Math.floor(sAll / (1000 * 60 * 60 * 24))
  const h = Math.floor((sAll - d * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))
  const m = Math.floor((sAll - d * (1000 * 60 * 60 * 24) - h * (1000 * 60 * 60)) / (1000 * 60))
  const s = Math.floor((sAll - d * (1000 * 60 * 60 * 24) - h * (1000 * 60 * 60) - m * (1000 * 60)) / 1000)
  let result: any = {d, h, m ,s}
  if (format) {
    result = ''
    if (d > 0) {
      result += d + '天'
    }
    if (d > 0 || h > 0) {
      result += h + '时'
    }
    if (d > 0 || h > 0 || m > 0) {
      result += m + '分'
    }
    result += s + '秒'
  }
  return result
}

export const comTimeByArea = (start: any, end: any, format = false, _D = '天', _H = '时', _M = '分', _S = '秒') => {
  const sAll = new Date(end).getTime() - new Date(start).getTime()
  const d = Math.floor(sAll / (1000 * 60 * 60 * 24))
  const h = Math.floor((sAll - d * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))
  const m = Math.floor((sAll - d * (1000 * 60 * 60 * 24) - h * (1000 * 60 * 60)) / (1000 * 60))
  const s = Math.floor((sAll - d * (1000 * 60 * 60 * 24) - h * (1000 * 60 * 60) - m * (1000 * 60)) / 1000)
  let result: any = {d, h, m ,s}
  if (format) {
    result = ''
    if (d > 0) {
      result += d + _D
    }
    if (d > 0 || h > 0) {
      result += h + _H
    }
    if (d > 0 || h > 0 || m > 0) {
      result += m + _M
    }
    result += s + _S
  }
  return result
}

export const deepAssign = (...obj: any) => {
  const result = Object.assign({}, ...obj)
  for (let item of obj) {
    for (let [idx, val] of Object.entries(item)) {
      if (val instanceof Array) {
        result[idx] = val
      } else if (val instanceof Object) {
        result[idx] = deepAssign(result[idx], val)
      }
    }
  }
  return result
}

export const copy = (value: any) => {
  const str = document.createElement('input')
  str.setAttribute('value', value)
  document.body.appendChild(str)
  str.select()
  document.execCommand('copy')
  document.body.removeChild(str)
  console.log(value)
}

/**
 *
 * @param precision 精度  1、0.1 、0.01……
 * @param colorArr
 * [
 *    [20.1, '#111111'],
 *    [20.3, '#dddddd'],
 *    [20.7, '#eeeeee'],
 * ]
 * @return colorMap
 * new Map([
 *    [20.1, '#111111']
 *    ……
 *    [20.3, '#dddddd']
 *    ……
 *    [20.7, '#eeeeee']
 * ])
 */
export const getGradientColorArray = (precision: any, colorArr: any) => {
  // 将hex表示方式转换为rgb表示方式(这里返回rgb数组模式)
  const colorRgb = (sColor: any) => {
    const reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
    let _sColor = sColor.toLowerCase();
    if (_sColor && reg.test(_sColor)) {
      if (_sColor.length === 4) {
        let sColorNew = "#";
        for (let i = 1; i < 4; i += 1) {
          sColorNew += _sColor.slice(i, i + 1).concat(_sColor.slice(i, i + 1));
        }
        _sColor = sColorNew;
      }
      //处理六位的颜色值
      const sColorChange = [];
      for (let i = 1; i < 7; i += 2) {
        // @ts-ignore
        sColorChange.push(parseInt("0x" + _sColor.slice(i, i + 2)));
      }
      return sColorChange;
    } else {
      return _sColor;
    }
  };
  // 将rgb表示方式转换为hex表示方式
  const colorHex = (rgb: any) => {
    const _this = rgb;
    const reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
    if (/^(rgb|RGB)/.test(_this)) {
      const aColor = _this.replace(/(?:(|)|rgb|RGB)*/g, "").split(",");
      let strHex = "#";
      for (let i = 0; i < aColor.length; i++) {
        let hex = Number(aColor[i]).toString(16);
        hex = Number(hex) < 10 ? 0 + '' + hex : hex;// 保证每个rgb的值为2位
        if (hex === "0") {
          hex += hex;
        }
        strHex += hex;
      }
      if (strHex.length !== 7) {
        strHex = _this;
      }
      return strHex;
    } else if (reg.test(_this)) {
      const aNum = _this.replace(/#/, "").split("");
      if (aNum.length === 6) {
        return _this;
      } else if (aNum.length === 3) {
        let numHex = "#";
        for (let i = 0; i < aNum.length; i += 1) {
          numHex += (aNum[i] + aNum[i]);
        }
        return numHex;
      }
    } else {
      return _this;
    }
  }
  const rgb2hex = (sRGB: any) => {
    const reg = /^(RGB|rgb)\((\d+),\s*(\d+),\s*(\d+)\)$/
    if (!reg.test(sRGB)) return sRGB
    const rgbArr = sRGB.match(/\d+/g)
    const resultRgbArr = rgbArr.map((v: any) => {
      if (+v > 16) return (+v).toString(16)
      return '0' + (+v).toString(16)
    })
    return '#' + resultRgbArr.join('')
  }
  const gradientColor = (startColor: any, endColor: any, step: any) => {
    const startRGB = colorRgb(startColor);//转换为rgb数组模式
    const startR = startRGB[0];
    const startG = startRGB[1];
    const startB = startRGB[2];
    const endRGB = colorRgb(endColor);
    const endR = endRGB[0];
    const endG = endRGB[1];
    const endB = endRGB[2];
    const sR = (endR - startR) / step;//总差值
    const sG = (endG - startG) / step;
    const sB = (endB - startB) / step;
    const colorArr = [];
    for (let i = 0; i <= step; i++) {
      //计算每一步的hex值
      const hex = colorHex('rgb(' + parseInt((sR * i + startR)) + ',' + parseInt((sG * i + startG)) + ',' + parseInt((sB * i + startB)) + ')');
      // @ts-ignore
      colorArr.push(rgb2hex(hex));
    }
    return colorArr;
  }
  const colorMap = new Map()
  colorArr.forEach((v: any, i: number) => {
    if (i < colorArr.length - 1) {
      const _arr = gradientColor(v[1], colorArr[i + 1][1], (Number(colorArr[i + 1][0]) - Number(v[0])) / precision)
      _arr.forEach((cV, cI) => {
        colorMap.set((Number(v[0]) + cI * precision).toFixed(String(precision).split('').filter(p => p === '0').length), cV)
      })
    } else {
      colorMap.set(Number(v[0]).toFixed(String(precision).split('').filter(p => p === '0').length), v[1])
    }
  })
  return colorMap
}
// 根据部门名称 寻找上级所有父节点名称
//data：要遍历的数据， target：查找目标， result用于装查找结果的数组
export const findParent = (data: any, target: any, result: any) => {
  for (let item of data) {
    if (item.deptName === target) {
      //将查找到的目标数据加入结果数组中
      //可根据需求unshift(item.id)或unshift(item)
      result.unshift(item.deptName);
      return true;
    }
    if (item.children && item.children.length > 0) {
      let isFind = findParent(item.children, target, result);
      if (isFind) {
        result.unshift(item.deptName);
        return true;
      }
    }
  }
  return false;
};

// 涉及到跨域问题，需要配nginx代理的url地址处理
export const proxyNginxUrl = (url: string) => {
  if (url) {
    //  @ts-ignore
    const apiMapper = window.czrConfig?.nginxApiMapper || new Map()
    let newUrl = url
    apiMapper.forEach((v: any, k: any) => {
      if (url.includes(k)) {
        newUrl =  v + url.substring(url.indexOf(k) + k.length, url.length)
      }
    })
    return newUrl
  }
  return url
};

//  后端接口拦截 <  > 内的文本，正反向格式化为  /《/   /》/    res = true 为接收返回值进行格式化
export const formatInputHtmlInterceptor = (html: string, res = true) => {
  if (html) {
    const map = new Map()
    map.set('<', '_《_')
    map.set('>', '_》_')
    let nHtml = html.toString()
    map.forEach((v, k) => {
      if (res) {
        nHtml = nHtml.replace(eval(`/${v}/g`), k)
      } else {
        nHtml = nHtml.replace(eval(`/${k}/g`), v)
      }
    })
    return nHtml
  }
  return html
}



//生成从minNum到maxNum的随机数
// export  const randomNum = (minNum: number,maxNum: number) => {
//   return parseInt(String(Math.random() * (maxNum - minNum + 1) + minNum),10);
// }

export const randomNum = (min = 0, max = 0, decimal=0) => {
  // 获取数值的小数部分
  const getDecimalNum = (data: number) => {
    return Number(data.toString().split('.')[1]);
  }
  let min_z = Math.trunc(min); // 最小值的整数部分
  let max_z = Math.trunc(max); // 最大值的整数部分
  // 判断是否存在小数部分，不存在的话为0
  let min_x = isNaN(getDecimalNum(min)) ? 0 : getDecimalNum(min);  // 最小值的小数部分
  let max_x = isNaN(getDecimalNum(max)) ? 0 : getDecimalNum(max);  // 最大值的小数部分

  // 区分有小数和没小数的情况
  if (min_x > 0 || max_x > 0 || decimal > 0) {
    // 整数部分随机数
    let z = parseInt(String(Math.random() * (max_z - min_z + 1) + min_z), 10);
    // 小数部分随机数
    let x = 0;
    // 小数部分随机数最大位数
    let max_decimal = min_x.toString().length > max_x.toString().length ? min_x.toString().length : max_x.toString().length;
    max_decimal = decimal > max_decimal ? decimal : max_decimal;
    // 判断随机出的整数部分，是否等于最小值或者最大值
    if(z == min_z || z == max_z){
      if(z == min_z){
        // 整数部分随机数等于最小值，那么应该从最小值的小数部分开始，到小数位数的最大值随机就可以
        x = parseInt(String(Math.random() * (Math.pow(10, max_decimal) - min_x) + min_x), 10);
      }else{
        // 整数部分随机数等于最大值，那么应该从0开始，到最大值小数部分
        x = parseInt(String(Math.random() * (max_x + 1)), 10);
      }
    }else{
      // 整数部分在最大最小值区间的，就从0到小数位数的最大值随机就可以
      x = parseInt(String(Math.random() * (Math.pow(10, max_decimal))), 10);
    }
    return Number(`${z}.${x}`);
  } else {
    return parseInt(String(Math.random() * (max_z - min_z + 1) + min_z), 10);
  }
}

/**
 *
 * @param val 处理的数值
 * @param unit  单位，默认空字符
 * @param digits  小数，默认1
 */
export const formatNumberUnit = (val, unit = '', digits = 1) => {
  let num = ''
  let str = ''
  if (String(val).length < 5) {
    num = val
    str = unit
  } else if (String(val).length < 9) {
    num = Number((Number(val) / 10000).toFixed(digits)) / 1 + ''
    str = '万' + unit
  } else if (String(val).length < 13) {
    num = Number((Number(val) / 10000 / 10000).toFixed(digits)) / 1 + ''
    str = '亿' + unit
  }
  return {
    num: num, unit: str
  }
}

/**
 * 将一串数字str按指定间隔n填充指定字符char
 * @param str
 * @param n
 * @param char
 */
export const numberJoinChar = (str, n = 3, char = ',') => {
  const _str = String(str)
  let result = '';
  for (let i = _str.length - 1; i >= 0; i--) {
    result = _str[i] + result;
    if ((_str.length - i) % n === 0 && i !== 0) {
      result = char + result;
    }
  }
  return result;
}
// 根据起始周几获取本周第一天日期
export const getFirstDateOfWeek = (fDate, firstDayOfWeek) => {
  const day = fDate.getDay();
  const diff = (day < firstDayOfWeek ? 7 : 0) + day - firstDayOfWeek;
  const firstDate = new Date(fDate.getFullYear(), fDate.getMonth(), fDate.getDate() - diff);
  return firstDate;
}

// 根据起始周几获取本周最后一天日期
export const getLastDateOfWeek = (lDate, lastDayOfWeek) => {
  const day = lDate.getDay();
  const diff = (day < lastDayOfWeek ? 7 : 0) + day - lastDayOfWeek;
  const lastDate = new Date(lDate.getFullYear(), lDate.getMonth(), lDate.getDate() + (6 - diff));
  return lastDate;
}

export const oneDayTime = 1000 * 60 * 60 * 24

//  根据当天时间、起始周几、选的月，获取整月日期
export const getMonthCalendarData = (d, fD, selectMonth = null) => {
  const getYMDTime = (date) => {
    const _date = new Date(date)
    const Y = _date.getFullYear()
    const M = _date.getMonth() + 1
    const D = _date.getDate()
    return new Date(`${Y}-${M}-${D}`).getTime()
  }
  const _date = selectMonth ? new Date(selectMonth) : new Date(d)
  // d的月份有几天
  const monthDaysTotal = new Date(_date.getFullYear(), _date.getMonth() + 1, 0).getDate()
  // d的月份的第一天
  const monthFirstDate = new Date(_date.getFullYear(), _date.getMonth(), 1)
  // 日历第一天
  const wholeFirstDay = getFirstDateOfWeek(monthFirstDate, fD)
  // d的月份的最后一天
  const monthLastDate = new Date(_date.getFullYear(), _date.getMonth(), monthDaysTotal)
  // 日历最后一天
  const wholeLastDay = getLastDateOfWeek(monthLastDate, fD)
  const arr: any = []
  let w: any = []
  for (let i = wholeFirstDay.getTime(); i <= wholeLastDay.getTime(); i += oneDayTime) {
    const t = new Date(i)
    const obj: any = {
      date: t,
      dateStr: YMD(t),
      dayStr: t.getDate(),
      tom: getYMDTime(t) > getYMDTime(d)
    }
    if (getYMDTime(t) < monthFirstDate.getTime()) {
      obj.last = true //  是补全的日历过去时间
    } else if (getYMDTime(t) > monthLastDate.getTime()) {
      obj.will = true //  是补全的日历未来时间
    } else if (getYMDTime(t) === getYMDTime(d)) {
      obj.today = true // 是传入的当前日期
    }
    w.push(obj)
    if (w.length === 7) {
      const wObj: any = {
        calendar: w
      }
      arr.push(wObj)
      w = []
    }
  }
  return arr
}

// 根据起始周几获取一周的数组
export const getWeekCN = (xq) => {
  const m = new Map([
    [0, '日'],
    [1, '一'],
    [2, '二'],
    [3, '三'],
    [4, '四'],
    [5, '五'],
    [6, '六'],
  ])
  const weekArray: any = [];
  for (let i = 0; i < 7; i++) {
    const index = (xq + i) % 7;
    weekArray.push(m.get(index));
  }
  return weekArray
}

export const randomColor = (opacity) => `rgba(${randomNum(0, 255)}, ${randomNum(0, 255)}, ${randomNum(0, 255)}, ${opacity ? opacity : randomNum(0.5, 1, 1)})`


 //判断是否覆盖24小时 
export const covers24Hours = (arr) =>{
  if (arr.length == 0) return false;
  let result = 0;
  for (const item of arr) {
    //获取时间
    if (item.length == 0) return;
    let start = item[0]?.split(":");
    let end = item[1]?.split(":");
    //把时间全部转化为秒
    start = Number(start[0]) * 3600 + Number(start[1]) * 60 + Number(start[2]);
    end = Number(end[0]) * 3600 + Number( end[1]) * 60 +  Number(end[2]);
    if (start > end) {
      end = end + 86400;
    }
    result += Math.abs(end - start);
  }
  return result === 86400 || result === 86399;
}

export const toPhone = (phone) => {
  let a: any = document.createElement('a')
  a.style = {opacity: 0}
  a.href = 'tel:' + phone
  a.click()
  document.removeChild(a)
}

export const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms))

export const hexToRgb = (hex) => {
  // 移除可能存在的 '#' 符号
  const cleanHex = hex.replace('#', '');
  // 确保长度为 6 位
  if (cleanHex.length !== 6) {
    throw new Error('Invalid hex color format');
  }
  // 解析 R, G, B 值
  const r = parseInt(cleanHex.substring(0, 2), 16);
  const g = parseInt(cleanHex.substring(2, 4), 16);
  const b = parseInt(cleanHex.substring(4, 6), 16);

  // 返回 RGB 字符串
  return `rgb(${r}, ${g}, ${b})`;
}

export const hexToOpaqueHex = (hex, opacity) => {
  // 解析 16 进制颜色
  const match = hex.match(/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i);
  if (!match) {
    throw new Error('Invalid hex color format');
  }

  // 提取 RGB 值
  const r = parseInt(match[1], 16);
  const g = parseInt(match[2], 16);
  const b = parseInt(match[3], 16);

  // 计算不透明的 RGB 值
  const oneMinusOpacity = 1 - opacity;
  const finalR = Math.round(r * opacity + 255 * oneMinusOpacity);
  const finalG = Math.round(g * opacity + 255 * oneMinusOpacity);
  const finalB = Math.round(b * opacity + 255 * oneMinusOpacity);

  // 将 RGB 值转换为 16 进制颜色
  return `#${finalR.toString(16).padStart(2, '0')}${finalG.toString(16).padStart(2, '0')}${finalB.toString(16).padStart(2, '0')}`;
}

export const extractRgbFromRgba = (rgbaString) => {
  const match = rgbaString.match(/^rgba?\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})(?:,\s*[\d.]+)?\)$/);
  if (match) {
    return [parseInt(match[1], 10), parseInt(match[2], 10), parseInt(match[3], 10)];
  }
  return [46, 129, 255];
}

export const _console = (p) => console.log(p)

export const resetRgbaOpacity = (originalColor, opacity) => {
  const parseColor = (colorStr) => {
    colorStr = colorStr.trim().toLowerCase();
    if (colorStr.startsWith('#')) {
      return parseHex(colorStr);
    } else if (colorStr.startsWith('rgb')) {
      return parseRgba(colorStr);
    } else {
      throw new Error('Invalid color format');
    }
  }
  const parseHex = (hexStr) => {
    let hex = hexStr.replace('#', '');
    let len = hex.length;
    if (len === 3 || len === 4) {
      hex = hex.split('').map(c => c + c).join('');
    }
    if (hex.length === 6) hex += 'ff';
    else if (hex.length === 8) {}
    else {
      throw new Error('Invalid hex color');
    }
    const r = parseInt(hex.substring(0, 2), 16);
    const g = parseInt(hex.substring(2, 4), 16);
    const b = parseInt(hex.substring(4, 6), 16);
    const a = parseInt(hex.substring(6, 8), 16) / 255;
    return {
      r: clamp(r, 0, 255),
      g: clamp(g, 0, 255),
      b: clamp(b, 0, 255),
      a: clamp(a, 0, 1)
    };
  }
  const parseRgba = (rgbaStr) => {
    const regex = /^rgba?\(\s*([\d.]+)(%?)\s*,\s*([\d.]+)(%?)\s*,\s*([\d.]+)(%?)\s*(?:,\s*([\d.]+)(%?)\s*)?\)$/;
    const match = rgbaStr.match(regex);
    if (!match) throw new Error('Invalid RGB/RGBA format');
    const [_, rVal, rUnit, gVal, gUnit, bVal, bUnit, aVal, aUnit] = match;
    const r = parseComponent(rVal, rUnit);
    const g = parseComponent(gVal, gUnit);
    const b = parseComponent(bVal, bUnit);
    let a = 1;
    if (aVal !== undefined) {
      a = aUnit === '%' ? parseFloat(aVal) / 100 : parseFloat(aVal);
      a = clamp(a, 0, 1);
    }
    return { r, g, b, a };
  }
  const parseComponent = (valueStr, unit) => {
    let value = parseFloat(valueStr);
    if (unit === '%') {
      value = (value * 255) / 100;
    }
    return clamp(value, 0, 255);
  }
  const clamp = (value, min, max) => {
    return Math.min(max, Math.max(min, value));
  }
  // 入口
  const parsed = parseColor(originalColor);
  const newAlpha = Math.max(0, Math.min(1, parsed.a * opacity));
  return `rgba(${parsed.r}, ${parsed.g}, ${parsed.b}, ${newAlpha})`;
}

function getContrastColor(inputColor) {
  // 解析颜色为RGBA对象
  const parseColor = (color) => {
    const div = document.createElement('div');
    div.style.color = color;
    document.body.appendChild(div);
    const computed = getComputedStyle(div).color;
    document.body.removeChild(div);
    const rgba = computed.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*([\d.]+))?\)/);
    if (!rgba) return { r: 0, g: 0, b: 0, a: 1 };
    return {
      r: parseInt(rgba[1], 10),
      g: parseInt(rgba[2], 10),
      b: parseInt(rgba[3], 10),
      a: rgba[4] ? parseFloat(rgba[4]) : 1
    };
  };

  // 格式化为与原色相似的字符串
  const formatColor = (color, original) => {
    const isHex = original.startsWith('#');
    const hasAlpha = original.includes('a') || (original.startsWith('#') && original.length === 9);
    if (isHex) {
      const toHex = (v) => Math.round(v).toString(16).padStart(2, '0');
      let hex = `#${toHex(color.r)}${toHex(color.g)}${toHex(color.b)}`;
      if (hasAlpha) hex += toHex(color.a * 255);
      return hex.toUpperCase();
    } else {
      return color.a === 1
        ? `rgb(${color.r}, ${color.g}, ${color.b})`
        : `rgba(${color.r}, ${color.g}, ${color.b}, ${color.a})`;
    }
  };

  const parsed = parseColor(inputColor);

  // 方法1：生成补色（反转RGB并保留透明度）
  const inverse = {
    r: 255 - parsed.r,
    g: 255 - parsed.g,
    b: 255 - parsed.b,
    a: parsed.a
  };

  // 方法2：根据亮度选择黑/白（忽略透明度确保对比度）
  const luminance = (0.299 * parsed.r + 0.587 * parsed.g + 0.114 * parsed.b) / 255;
  const bw = luminance > 0.5
    ? { r: 0, g: 0, b: 0, a: 1 }
    : { r: 255, g: 255, b: 255, a: 1 };

  // 选择对比度更高的方案
  const getContrastRatio = (c1, c2) => {
    const l1 = (0.299 * c1.r + 0.587 * c1.g + 0.114 * c1.b) / 255;
    const l2 = (0.299 * c2.r + 0.587 * c2.g + 0.114 * c2.b) / 255;
    return (Math.max(l1, l2) + 0.05) / (Math.min(l1, l2) + 0.05);
  };

  const inverseContrast = getContrastRatio(parsed, inverse);
  const bwContrast = getContrastRatio(parsed, bw);

  // 返回对比度更高的颜色，若补色不足则用黑/白
  const finalColor = inverseContrast >= bwContrast ? inverse : bw;
  return formatColor(finalColor, inputColor);
}